package com.example.lastwork2.fragments;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.lastwork2.R;
import com.example.lastwork2.adapter.HomeAdapter;
import com.example.lastwork2.fragments.recfragments.RecClothFragment;
import com.example.lastwork2.fragments.recfragments.RecPhoneFragment;
import com.example.lastwork2.fragments.recfragments.RecRecFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class RecommendFragment extends Fragment {

    //声明标签
    private TabLayout reTab;
    private ViewPager2 reVp2;
    //声明适配器对象
    private HomeAdapter homeAdapter;

    //声明子页面资源
    private List<Fragment> fragments;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //绑定布局文件
        View recommendPage = inflater.inflate(R.layout.recommend_fragment,null);

        //绑定控件
        reTab = recommendPage.findViewById(R.id.recommend_tab);
        reVp2 = recommendPage.findViewById(R.id.recommend_vp2);

        //初始化子页面资源
        fragments = new ArrayList<>();
        fragments.add(new RecRecFragment());
        fragments.add(new RecPhoneFragment());
        fragments.add(new RecClothFragment());

        //初始化子页面适配器对象
        homeAdapter = new HomeAdapter(fragments,this);
        reVp2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        //绑定适配器
        reVp2.setAdapter(homeAdapter);
        //关联tab 和 vp
        TabLayoutMediator mediator = new TabLayoutMediator(
                reTab,
                reVp2,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        //设置标签的名称
                        switch (position) {
                            case 0:
                                tab.setText("推荐");
                                break;
                            case 1:
                                tab.setText("手机");
                                break;
                            case 2:
                                tab.setText("衣服");
                                break;
                        }
                    }
                }
        );
        mediator.attach();



        return recommendPage;
    }
}